package com.tchy.cloud.tools;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

/**
 * Created by zhongjieyu on 2014/9/1.
 */
public class Tools {

    /**
     * 获取系统格式化时间字符串
     *
     * @return 时间
     */
    public static String getFormatSystemTime() {


        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");

        String time = sdf.format(new Date());

        return time;
    }

    /**
     * 获取格式化后的星期
     *
     * @return
     */
    public static String getFormatWeek() {


        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        int week = cal.get(Calendar.DAY_OF_WEEK);

        String formatWeek[] = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};

        return formatWeek[week];
    }


    /**
     * 创建文件,如果文件名重复则自动更改文件名
     *
     * @param path     文件路径
     * @param fileName 文件名
     * @param count    默认传入初始次数 2
     * @return 返回新创建的文件
     * @throws Exception
     */
    public static File getFileByOriginalFileName(String path, String fileName, int count) throws Exception {

        File targetFile = new File(path, fileName);


        if (!targetFile.exists()) {
            targetFile.mkdirs();
        } else {


            String new_fileName = fileName.substring(0, fileName.lastIndexOf("."));

            if (count != 2) {

                new_fileName = fileName.substring(0, fileName.lastIndexOf("(" + (count - 1) + ")"));


            }

            new_fileName += "(" + count + ")";

            //加入文件后缀
            if (fileName != null && !"".equals(fileName) && fileName.lastIndexOf(".") != -1) {

                new_fileName += fileName.substring(fileName.lastIndexOf("."), fileName.length()).toLowerCase();

            }

            count++;
            //递归，调用自身
            targetFile = getFileByOriginalFileName(path, new_fileName, count);


        }

        return targetFile;

    }

    /**
     * 随机生成6位数字组成的字符串
     * xmweilove
     * @return
     */
    public static String getRandomNum() {
        int[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
        Random rand = new Random();
        for (int i = 10; i > 1; i--) {
            int index = rand.nextInt(i);
            int tmp = array[index];
            array[index] = array[i - 1];
            array[i - 1] = tmp;
        }
        int result = 0;
        for (int i = 0; i < 8; i++)
            result = result * 10 + array[i];
        return String.valueOf(result);
    }

    public static void main(String[] args) {
       System.out.println(Tools.getRandomNum());
    }
}
